package weifei.wx.community.db.service;


import lombok.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import weifei.wx.community.db.mapper.DBMapper;

import java.util.List;
import java.util.Map;


@Service
public class DBService
{
    @Autowired
    private DBMapper dbMapper;

    @Getter
    private int pageSize = 10;


    public boolean setPageSize(int size) {
        if (size <= 0)
            return false;
        this.pageSize = size;
        return true;
    }


    /**
     * @return 返回 table 记录数 的总页数
     * */
    public int getPageCount(String tableName) {
        int dataCount = this.selectCount(tableName);
        if (dataCount <= this.pageSize)
            return 1;
        int pageCount = dataCount / this.pageSize;
        int remainder = dataCount % this.pageSize;
        if (remainder > 0)
            pageCount++;
        return pageCount;
    }


    public List<String> selectAllTableNameList() {
        return this.dbMapper.selectAllTableNameList();
    }


    public List<String> selectAllTableColumnNameList(String tableName) {
        return this.dbMapper.selectAllTableColumnNameList(tableName);
    }


    public Map<String, Object> selectColumnKeyUsage(String tableName, String columnName) {
        return this.dbMapper.selectColumnKeyUsage(tableName, columnName);
    }


    public String selectColumnDefaultValue(String tableName, String columnName) {
        return this.dbMapper.selectColumnDefaultValue(tableName, columnName);
    }


    public boolean selectColumnIsNullable(String tableName, String columnName) {
        var is = this.dbMapper.selectColumnIsNullable(tableName, columnName);
        return is.equals("YES");
    }

    public String selectColumnDataType(String tableName, String columnName) {
        return this.dbMapper.selectColumnDataType(tableName, columnName);
    }


    /**
     * @return 返回 table 记录数
     * */
    public int selectCount(String tableName) {
        return this.dbMapper.selectCount(tableName);
    }


    /**
     * @param pageIndex 从 1 开始
     * */
    public List<Map<String, Object>> selectPage(String tableName, int pageIndex)
    {
        if (pageIndex <= 0)
            return null;
        var startIndex = this.pageSize * (pageIndex - 1);
        return this.dbMapper.selectLimit(tableName, startIndex, this.pageSize);
    }


    public List<Map<String, Object>> searchByColumnValue(String tableName, String columnName, String value)
    {
        return this.dbMapper.searchByColumnValue(tableName, columnName, value);
    }


    public int insert(String tableName, Map<String, Object> map)
    {
        return this.dbMapper.insert(tableName, map);
    }
    public int updateByID(String tableName, int id, Map<String, Object> map)
    {
        if (id == 0)
            return 0;
        return this.dbMapper.updateByID(tableName, id, map);
    }
    public int deleteByID(String tableName, int id)
    {
        return this.dbMapper.deleteByID(tableName, id);
    }
}


